package com.example.demo.t2.buffer;

import javax.swing.plaf.IconUIResource;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.StandardCharsets;
import java.util.*;

public class WriteFile {
    public static HashMap<String, Integer> CalKeyCount(String cnt) {
        StringTokenizer stringTokenizer = new StringTokenizer(cnt);
        HashMap<String, Integer> wordCount = new HashMap<>();
        while (stringTokenizer.hasMoreTokens()) {
            String word = stringTokenizer.nextToken();
            InWord(wordCount, word, 1);
        }
        return wordCount;
    }
    static void InWord(HashMap<String, Integer> hashMap, String word, Integer count) {
        if (hashMap.containsKey(word)) {
            hashMap.put(word, hashMap.get(word) + count);
        } else {
            hashMap.put(word, count);
        }
    }
    static void StatsWords() throws FileNotFoundException, IOException {
        BufferedInputStream fs = new BufferedInputStream(new FileInputStream("word.txt"));
        byte[] bytes = new byte[8192];
        HashMap<String, Integer> wordStats = new HashMap<>();
        while(fs.read(bytes) != -1){
            var bBuff = ByteBuffer.allocate(8192);
            bBuff.put(bytes);
            bBuff.flip();
            var cBuff = CharBuffer.allocate(8192);
            StandardCharsets.US_ASCII.newDecoder().decode(bBuff, cBuff, true);
            var wordCount = CalKeyCount(new String(cBuff.array()));
            wordCount.keySet().forEach(word -> InWord(wordStats, word, wordCount.get(word)));
        }
        wordStats.keySet().stream().map(o -> {
            System.out.println(o + ":" + wordStats.get(o));
            return null;
        });
        wordStats.keySet().forEach(o -> {
            System.out.println(o + ":" + wordStats.get(o));
        });
        System.out.println(wordStats.size());
    }


    public static void main(String[] args) throws FileNotFoundException, IOException {

        Stream.of()
        Optional.of()


//        var random = new Random();
//        var fout = new BufferedOutputStream(new FileOutputStream("word.txt"));
//        var t0 = System.currentTimeMillis();
//        for(var i=0; i<1_000_000; i++){
//            for(var j=0; j<5; j++){
//                fout.write(97 + random.nextInt(5));
//            }
//            fout.write(' ');
//        }
//        System.out.println(System.currentTimeMillis() - t0);

//        ReadFileWithNIO();

//        StringTokenizer stringTokenizer = new StringTokenizer("abc bb dd aeee");
//        while(stringTokenizer.hasMoreTokens()) {
//            System.out.println(stringTokenizer.nextToken());
//        }

//        ReadChinese();

        StatsWords();
    }

    static void ReadChinese() {
        String txt = "测试一段话";
        var charset = StandardCharsets.UTF_8;
        ByteBuffer bBuff = ByteBuffer.allocate(11);
        CharBuffer cBuff = CharBuffer.allocate(11);
        byte[] bytes = charset.encode(txt).array();
        bBuff.put(bytes, 0, 11);
        System.out.println(bBuff.limit() - bBuff.position());
        bBuff.flip();
        System.out.println(bBuff.limit() - bBuff.position());
//        Arrays.copyOfRange(bytes, 0, 11);
        charset.newDecoder().decode(bBuff, cBuff, true);
//        cBuff.flip();

        System.out.println(bBuff.limit() - bBuff.position());
        System.out.println(new String(cBuff.array()));
    }

    static void ReadFileWithNIO() throws FileNotFoundException, IOException {
        var fc = new FileInputStream("word.txt").getChannel();
        ByteBuffer buff = ByteBuffer.allocate(8192);
        var i =0;
        var t0 = System.currentTimeMillis();
        while( (fc.read(buff)) > -1) {
            i++;
            buff.flip();
            // do read from buff
            System.out.println(new String(buff.array()));
            buff.clear();
        }
        System.out.println(i);
        System.out.println(System.currentTimeMillis() - t0);
        fc.close();
    }
}
